package com.iterator.controller.web.marketManager;

import com.iterator.bean.web.lc.BrandBean.BrandCreateBO;
import com.iterator.bean.web.lc.BrandBean.BrandUpdateBO;
import com.iterator.bean.web.lc.BrandBean.BrandVO;
import com.iterator.bean.web.lc.BrandBean.BrandListBO;
import com.iterator.bean.web.lc.BrandBean.BrandListVO;
import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.bean.web.pojo.Brand;
import com.iterator.service.web.marketManager.BrandServiceImpl;
import com.iterator.util.MoneyVaildateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RequestMapping("admin/brand")
@RestController
public class BrandController {

    @Autowired
    BrandServiceImpl brandService;
    @RequestMapping("list")
    @RequiresPermissions("admin:brand:list")
    public BaseRespVo brandList(BrandListBO brandListBO,BindingResult result){
        if(result.hasFieldErrors()){
            return BaseRespVo.fail("id必须为数字");
        }
        BrandListVO brandListVO = brandService.brandList(brandListBO);
        return BaseRespVo.ok(brandListVO);
    }

    /**
     * TODO sortOrder不能自增
     */
    @RequestMapping("create")
    @RequiresPermissions("admin:brand:create")
    public BaseRespVo createBrand(@RequestBody @Valid BrandCreateBO createBO, BindingResult result){
        if(result.hasFieldErrors() || !MoneyVaildateUtil.moneyVaildate(createBO.getFloorPrice())){
            return BaseRespVo.fail("参数错误");
        }
        BrandVO createVO = brandService.createBrand(createBO);
        return BaseRespVo.ok(createVO);
    }
    @RequestMapping("update")
    @RequiresPermissions("admin:brand:update")
    public BaseRespVo updateBrand(@RequestBody @Valid BrandUpdateBO updateBO, BindingResult result){
        if(result.hasFieldErrors() || !MoneyVaildateUtil.moneyVaildate(updateBO.getFloorPrice())){
            return BaseRespVo.fail("参数错误");
        }
        BrandVO brandVO = brandService.update(updateBO);
        return BaseRespVo.ok(brandVO);
    }
    @RequestMapping("delete")
    @RequiresPermissions("admin:brand:delete")
    public BaseRespVo deleteBrand(@RequestBody Brand brand){

        brandService.delete(brand.getId());
        return BaseRespVo.ok();
    }
}
